package com.foreknow.dao.impl;

import com.foreknow.dao.BusinessDao;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Business;

import java.sql.SQLException;
import java.util.List;

public class BusinessDaoImpl extends BaseDAO implements BusinessDao {

    @Override
    public boolean bSave(Business business) throws SQLException {
        String sql = "insert into business(password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice) " +
                "values(?,?,?,?,?,?)";
        int i =jdbcTemplate.update(sql,business.getPassword(),business.getBusinessName(),business.getBusinessAddress(),business.getBusinessExplain(),
                business.getStarPrice(),business.getDeliveryPrice());
        return i==1;
    }

    @Override
    public boolean bDelete(int bid) throws SQLException {
        String sql = "delete from business where businessId=?";
        int isRight = jdbcTemplate.update(sql,bid);
        return isRight==1;
    }

    @Override
    public int bUpdate(Business b,int bid) throws SQLException {
        String sql = "update business set password=?,businessName=?,businessAddress=?,businessExplain=?," +
                "starPrice=?,deliveryPrice=? where businessId=?";
        int update = jdbcTemplate.update(sql, b.getPassword(), b.getBusinessName(), b.getBusinessAddress(),
                b.getBusinessExplain(), b.getStarPrice(), b.getDeliveryPrice(), bid);
        return update;
    }

    @Override
    public List<Object> bQueryAll(Business business) throws SQLException {
        String sql = "select * from business";
        List<Object> list = jdbcTemplate.query(sql,mappingFactory.getMap(MappingFactory.BUSINESS_MAPPING));
        return list;
    }

    @Override
    public List<Object> bQueryById(String bName) throws SQLException {
        String sql = "select * from business where businessName like concat('%',?,'%')";
        List<Object> list = jdbcTemplate.query(sql,mappingFactory.getMap(MappingFactory.BUSINESS_MAPPING),bName);
        return list;
    }
}
